//
//  SearchBarViewController.m
//  iOS UI Sample Code
//
//  Created by 규성 이 on 12. 2. 21..
//  Copyright (c) 2012년 kkamci77@gmail.com. All rights reserved.
//

#import "SearchBarViewController.h"

@implementation SearchBarViewController

@synthesize sbSearchBar, tvTableSample, aListData, aSearchResultListData, searchMode;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.sbSearchBar setDelegate:self];
    self.searchMode = NO;
    [self.navigationItem setTitle:@"Simple Search"];
    [self initListData];
//    [self.tvTableSample setSeparatorColor:[UIColor blueColor]];
//    self tvTableSample set
//    self.tvTableSample setS
 //   [self.tvTableSample setSeparatorStyle:UITableViewCellSeparatorStyleSingleLineEtched];
//    self.tvTableSample setSe
//    self.tvTableSample.in
    
//    UIImageView * iv = [[UIImageView alloc] init];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [sbSearchBar release];
    [tvTableSample release];
    [aListData release];
//    [aSearchResultListData release];
    [super dealloc];
}

-(void) initListData
{
    self.aListData = [[[NSMutableArray alloc] init] autorelease];
    for(int count=0; count<3; count++)
    {
        [self.aListData addObject: [NSString stringWithFormat:@"%d", count]];
    }
}

#pragma mark - Table view data source Delegate Implements
// Require Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [self.aListData count];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //cell setContentScaleFactor:<#(CGFloat)#>
    
    [cell.contentView setFrame:CGRectMake(cell.contentView.frame.origin.x - 7.0f, cell.contentView.frame.origin.y, cell.contentView.frame.size.width+14.0f, cell.contentView.frame.size.height)];
    NSLog(@"Contents View x=%f, y=%f, width=%f, height=%f", cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height);
    NSLog(@"x=%f, y=%f, width=%f, height=%f", cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
//    cell setFrame:CGRectMake(5.0f, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"memolistCell"];
    UITableViewCell *cell;
    if(indexPath.section == 0)
    {
        cell= [tableView dequeueReusableCellWithIdentifier:@"sampleTableCell"];
        
        if(!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"sampleTableCell" owner:self options:nil] lastObject];
            

        }
        
        cell.backgroundColor = [UIColor whiteColor];
        [(UILabel*) [cell viewWithTag:5] setText:@"TEST"];
//        cell.textLabel.text = [self.aListData objectAtIndex:indexPath.row];
    }else
    {
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            //cell = [[[NSBundle mainBundle] loadNibNamed:@"memolistCell" owner:self options:nil] lastObject];
        }
        cell.backgroundColor = [UIColor whiteColor];
        
        //    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        // Configure the cell...
        cell.textLabel.text = [self.aListData objectAtIndex:indexPath.row];
    }
    

//    UILabel * label = (UILabel *)[cell viewWithTag:2];
//    [label setText:((MemoData *)[self.listData objectAtIndex:[indexPath row]]).name];
//    
//    UIImageView * imView = (UIImageView *)[cell viewWithTag:1];
//    [imView setImage:[((MemoData *)[self.listData objectAtIndex:[indexPath row]]) getThumbnailImage]];
//    
//    UILabel * lblDate = (UILabel *)[cell viewWithTag:3];
//    [lblDate setText:[((MemoData *)[self.listData objectAtIndex:[indexPath row]]).createDate description]];
    return cell;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 100.0f;
//}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1.0f;
}

// Optional Method
/*
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1; // [self.sectionData count];
}
*/
/**
 * - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
 * 이 Method를 구현하면 자동으로 Swife하여 Delete 버튼이 표시되게 된다.
 */
/*
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSLog(@"Delete EditStyle = %d", editingStyle);
    }
    else if(editingStyle == UITableViewCellEditingStyleInsert)
    {
        NSLog(@"Edit Style is Insert");
    }else
    {
        NSLog(@"Edit Style is None");
    }
}
*/
/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
*/

#pragma UITableViewDelegate Implements
/*
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
/*
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
*/
/*
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
*/

#pragma UISearchBarDelegate
/*
 * Tells the delegate that the user changed the search text.
 *
 * Parameters
 * searchBar
 *   The search bar that is being edited.
 * searchText
 *   The current text in the search text field.
 * Discussion
 *   This method is also invoked when text is cleared from the search text field.
 */
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    
}

/*
 * Ask the delegate if text in a specified range should be replaced with given text.
 */
/*
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
}
*/
/*
 * Asks the delegate if editing should begin in the specified search bar.
 *
 * Parameters
 * searchBar
 *   The search bar that is being edited.
 * Return Value
 *   YES if an editing session should be initiated, otherwise, NO.
 */

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    NSLog(@"SearchBarShouldBeginEditing");
    self.searchMode = YES;
    return YES;
}


/**
 * Tells the delegate when the user begins editing the search text.
 *
 * Parameters
 * searchBar
 *   The search bar that is being edited.
 */

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    NSLog(@"SearchBarTextDidBeginEditing");
}


/**
 * Asks the delegate if editing should stop in the specified search bar.
 *
 * Parameters
 * searchBar
 *   The search bar that is being edited.
 * Return Value
 *   YES if editing should stop, otherwise NO.
 */
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"SearchBarShouldEndEditing");
    self.searchMode = NO;
    return YES; 
}


/**
 * Tells the delegate that the user finished editing the search text.
 *
 * Parameters
 * searchBar
 *   The search bar that is being edited.
 */
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"SearchBarTextDidEndEditing");
}

// Button Click Event
/*
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
{
    
}
*/
/*
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    
}
*/

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.aListData removeAllObjects];
    
    //self.sbSearchBar.text
    for(int count=0; count<100; count++)
    {
        NSString * str = [NSString stringWithFormat:@"%d", count];
        if([str hasPrefix:searchBar.text])
        {
            [self.aListData addObject:str];
        }
    }
    [self.tvTableSample reloadData];
    [self.sbSearchBar resignFirstResponder];
}

/*
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar
{
    
}
*/
/*
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
{
    
}
*/
@end
